package com.qianfeng.iot.blackip.satrtup;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.dc3.common.constant.CacheConstant;
import com.qianfeng.iot.blackip.dto.Dc3BlackIpDto;
import com.qianfeng.iot.blackip.events.CacheRefreshEvent;
import com.qianfeng.iot.blackip.service.BlackIpService;
import com.qianfeng.iot.openfeignblock.CacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Jackiechan on 2022/7/21 9:24
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Component
@EnableAsync
public class LoadIp2Cache {

    private boolean isLoad;

    private StreamBridge streamBridge;

    @Autowired
    public void setStreamBridge(StreamBridge streamBridge) {
        this.streamBridge = streamBridge;
    }

    private CacheClient cacheClient;

    private BlackIpService blackIpService;

    @Autowired
    public void setBlackIpService(BlackIpService blackIpService) {
        this.blackIpService = blackIpService;
    }

    @Autowired
    public void setCacheClient(CacheClient cacheClient) {
        this.cacheClient = cacheClient;
    }


    //    @PostConstruct
//    public void init() {
//
//    }


    @EventListener
    @Async
    public void onEvent(ContextRefreshedEvent event) {
        //  System.err.println("程序启动了");
        //查询数据,包括停用的和删除的
        synchronized (cacheClient) {
            if (!isLoad) {
                isLoad = true;
                initData();

            }
        }

    }

    @EventListener
    @Async
    public void onEvent(CacheRefreshEvent event) {
        initData();
    }

    private void initData() {
        List<Dc3BlackIpDto> allIp = blackIpService.findAllIp2Dto();
        HashSet keys = new HashSet<>();
        keys.add(CacheConstant.Entity.BLACK_IP);
        //删除redis中的数据
        cacheClient.delete(keys);
        //保存到redis
        cacheClient.sAdd(CacheConstant.Entity.BLACK_IP, allIp.stream().map(Dc3BlackIpDto::getIp).collect(Collectors.toSet()));
        //发送消息出去通知网关全部更新
        streamBridge.send("blackipchange", "");
    }


}
